﻿Public Class SoftWareAdd

    Private _typeid As New List(Of String)
    Public Property 软件分类类型() As List(Of String)
        Get
            _typeid.Clear()
            For Each gr As DataGridViewRow In Me.dgType.Rows
                If CBool(gr.Cells(0).Value) Then
                    _typeid.Add(gr.Cells("colNao").Value)
                End If
            Next
            Return _typeid
        End Get
        Set(ByVal value As List(Of String))
            For Each gr As DataGridViewRow In Me.dgType.Rows
                For Each Str As String In value
                    If CStr(gr.Cells("colNao").Value) = Str Then
                        gr.Cells(0).Value = True
                    End If
                Next
            Next
            _typeid = value
        End Set
    End Property

    Private _更新 As Boolean = False
    Public Property 更新() As Boolean
        Get
            Return _更新
        End Get
        Set(ByVal value As Boolean)
            _更新 = value
        End Set
    End Property

    Private _软件 As CSoftWare = New CSoftWare
    Public Property 软件() As CSoftWare
        Get
            Return _软件
        End Get
        Set(ByVal value As CSoftWare)
            _软件 = value
        End Set
    End Property

    Private Function checkDb() As Boolean

        If Me.txtId.Text.Trim = "" Then
            wys.util.Common.MessageBase.ShowMessage("编号不能不空")
            txtId.Focus()
            Return False
        End If

        If 软件分类类型.Count = 0 Then
            wys.util.Common.MessageBase.ShowMessage("必须选择一个软件类型")
            Me.dgType.Focus()
            Return False
        End If

        If Me.txtName.Text.Trim = "" Then
            wys.util.Common.MessageBase.ShowMessage("名称不能不空")
            txtName.Focus()
            Return False
        End If

        If Me.WysDropTextFile1.Text.Trim = "" Then
            wys.util.Common.MessageBase.ShowMessage("路径不能不空")
            btnSelect.Focus()
            Return False
        End If

        If Not System.IO.File.Exists(WysDropTextFile1.Text) Then
            wys.util.Common.MessageBase.ShowMessage("路径不存在")
            Me.btnSelect.Focus()
            Return False
        End If

        If Me.txtDescription.Text.Trim = "" Then
            wys.util.Common.MessageBase.ShowMessage("软件描述不能不空")
            txtDescription.Focus()
            Return False
        End If

        Return True

    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If checkDb() = False Then
            Return
        End If

        软件.Id = txtId.Text
        软件.Name = txtName.Text.TrimEnd
        软件.TypeId = 软件分类类型
        软件.IconPath = pb.Tag.ToString
        If chk相对路径.Checked Then
            软件.FilePathType = "0"
        Else
            软件.FilePathType = "1"
        End If

        软件.FilePath = Util.GetSaveFileName(Me.WysDropTextFile1.Text, CInt(软件.FilePathType))

        软件.IconpathType = "2"
        软件.IconPath = ""
        软件.Description = Me.txtDescription.Text.TrimEnd
        Dim tyU As New XMLSoftWare
        If Me.Text = "添加软件" Then
            tyU.Create(软件)
        Else
            tyU.Update(软件)
        End If
        更新 = True
        If Me.Text = "添加软件" Then
            RaiseEvent Updateparent()
            AddInit()
            Return
        Else
            wys.util.Common.MessageBase.ShowMessage("更新成功")
        End If

        DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        of1.FileName = ""
        If System.IO.File.Exists(Me.WysDropTextFile1.Text) Then
            of1.FileName = Me.WysDropTextFile1.Text
        Else
            Dim fendir As String = Util.GetCurrentFolderSoftWare
            If System.IO.Directory.Exists(fendir) Then
                of1.InitialDirectory = fendir
            Else
                of1.InitialDirectory = Util.GetCurrentFolderSoftWare
            End If
        End If

        If of1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.WysDropTextFile1.Text = of1.FileName
            If of1.FileName.ToLower.Contains(Util.GetCurrentFolderSoftWare.ToLower) Then
                Me.chk相对路径.Checked = True
            Else
                Me.chk相对路径.Checked = False
            End If
            If Me.txtName.Text.Trim = "" Then
                Me.txtName.Text = System.IO.Path.GetFileNameWithoutExtension(of1.FileName)
            End If
        End If
    End Sub

    Private Sub WysDropTextFile1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WysDropTextFile1.TextChanged
        If System.IO.File.Exists(Me.WysDropTextFile1.Text) Then
            pb.Image = Util.GetExeIcon(Me.WysDropTextFile1.Text)
        End If

        If Me.WysDropTextFile1.Text.ToLower.Contains(Util.GetCurrentFolderSoftWare.ToLower) Then
            Me.chk相对路径.Checked = True
        Else
            Me.chk相对路径.Checked = False
        End If
    End Sub

    Public Sub New(ByVal 软件 As CSoftWare)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Me._软件 = 软件
    End Sub

    Private Sub RefreshForm()
        Dim totalWidth As Integer = dgType.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)
        dgType.Width = totalWidth
        If dgType.DisplayedRowCount(False) <> dgType.RowCount Then
            totalWidth = totalWidth + SystemInformation.BorderSize.Width + SystemInformation.VerticalScrollBarWidth + dgType.Margin.Right * 2
            dgType.Width = dgType.Width + SystemInformation.VerticalScrollBarWidth + dgType.Margin.Right
        Else
            totalWidth = totalWidth + SystemInformation.BorderSize.Width * 2 + dgType.Margin.Left + dgType.Margin.Right
        End If
        Me.Width = totalWidth + 3 + dgType.Left
    End Sub

    Public Event Updateparent()
    Private Sub AddInit()

        软件分类类型 = _软件.TypeId
        Dim tey As New XMLSoftWare
        Me.txtId.Text = tey.GetMaxid
        Me.txtName.Text = ""
        Me.WysDropTextFile1.Text = ""
        Me.pb.Image = Nothing
        Me.pb.Tag = ""
        Me.txtDescription.Text = ""
        Me.chk相对路径.Checked = False

    End Sub

    Private Sub UpdateInit()
        Dim tey As New XMLSoftWare
        _软件 = tey.GetById(_软件.Id)
        软件分类类型 = _软件.TypeId
        Me.txtId.Text = _软件.Id
        Me.txtName.Text = _软件.Name
        If _软件.FilePathType = "0" Then
            Me.chk相对路径.Checked = True
        Else
            Me.chk相对路径.Checked = False
        End If
        Me.WysDropTextFile1.Text = Util.GetCurrentExePath(_软件.FilePath, CInt(_软件.FilePathType))
        If System.IO.File.Exists(Me.WysDropTextFile1.Text) Then
            Me.pb.Image = Util.GetExeIcon(Me.WysDropTextFile1.Text)
        End If
        Me.txtDescription.Text = 软件.Description

    End Sub

    Private Sub SoftWareAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim dt As DataTable = Util.GetSoftWareType("")

            Me.dgType.DataSource = dt

            For index As Integer = 1 To Me.dgType.ColumnCount - 1
                Me.dgType.Columns(index).ReadOnly = False
            Next

            If Me.Text = "添加软件" Then
                AddInit()
            Else
                UpdateInit()
            End If

            RefreshForm()

        Catch ex As Exception

        End Try
    End Sub

End Class
